<?php
/*******************************************************************************
 * $Date:: Jan 18, 2011 11:59:21 PM                                              $
 * $Revision:                                                                  $
 * $Author:: Tuan Duong                                                        $
 * $Comment::                                                                  $
 ******************************************************************************/ 
/**
 * Description here...
 * 
 * @package Lua
 * @subpackage Grid
 * @author Tuan Duong <bacduong[at]gmail[dot]com>
 * @version 
 */

abstract class Lua_Grid_Column_Abstract
{
    protected $title = null;
    protected $options = array();
    public $key;
    public $sortable = true;
    
    public function __construct($key = '', $options = array())
    {
        if (isset($options['title'])) {
            $this->title = $options['title'];
            unset($options['title']);
        } elseif ($this->title === null) {
            $this->title = ucwords(str_replace('_', ' ', $key));
        }
        if (isset($options['sortable'])) {
            $this->sortable = $options['sortable'];
            unset($options['sortable']);
        }
        $this->key = $key;
        $this->options = $options; 
    }
    
    /**
     * Render header of column
     * @param Zend_View_Abstract $view
     * @param unknown_type $nowrap
     * @return string
     */
    public function renderHeader(Zend_View_Abstract $view, $nowrap = true)
    {
        return $this->headerCell($view, $this->title, $this->key, $this->options, $this->sortable, $nowrap);
    }
    
    /**
     * Render cell in the header
     * @param Zend_View_Abstract $view
     * @param string $text
     * @param string $key
     * @param array $params
     * @param bool $sortable
     * @param bool $nowrap
     * @return string
     */
    private function headerCell(Zend_View_Abstract $view, $text, $key, $params = array(), $sortable = true, $nowrap = true)
    {
        $params = array_merge(Lua_Uri::getInstance()->getQueryAsArray(), $params);
        if ($nowrap) {
            $html = '    <th nowrap="nowrap">' . $view->escape($text);
        } else {
            $html = '    <th><div>';
        }
        if ($sortable) {
            $html .= '<span class="sort"> ' . "\n";
            if (!isset($params['sort']) || ($params['sort'] != $key || $params['dir'] != 'asc')) {
                $html .= '<a class="up" href="' . $view->url(array('sort' => $key, 'dir' => 'asc')) . '">' . $view->escape($text) . '</a>';
            } else {
                if (!isset($params['sort']) || ($params['sort'] != $key || $params['dir'] != 'desc')) {
                    $html .= '<a class="down" href="' . $view->url(array('sort' => $key, 'dir' => 'desc')) . '">' . $view->escape($text) . '</a>';
                }
            }
            $html .= '</span>' . "\n";
        }
        $html .=  '    </div></th>' . "\n";
        return $html;
    }
    
    /**
     * Render cell in grid
     * @param string $value
     * @param array $row value of this row
     */
    abstract public function renderCell($value = '', $row = array());
    
    protected function cell($value)
    {
        $html = '    <td>';
        $html .= $value;
        $html .= '    </td>' . "\n";
        return $html;
    }
}